package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.dao.LitemallMerchandiseMapper;
import org.linlinjava.litemall.db.dao.LitemallMerchandiseMapperEx;
import org.linlinjava.litemall.db.domain.LitemallMerchandise;
import org.linlinjava.litemall.db.domain.LitemallMerchandiseExample;
import org.linlinjava.litemall.db.domain.MerchandiseStatisticsVo;
import org.linlinjava.litemall.db.vo.shopmerchandise.ConsumerStatisticsVO;
import org.linlinjava.litemall.db.vo.shopmerchandise.InNumStatisticsVO;
import org.linlinjava.litemall.db.vo.shopmerchandise.LossStatisticsVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

 

/**
 * litemall商品服务
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Service
@Transactional
public class LitemallMerchandiseService extends MPJBaseServiceImpl<LitemallMerchandiseMapper,LitemallMerchandise>  {
    /**
     * litemall商品映射器
     */
    @Resource
    private LitemallMerchandiseMapper litemallMerchandiseMapper;
    /**
     * litemall商品mapper交货
     */
    @Resource
    private LitemallMerchandiseMapperEx litemallMerchandiseMapperEx;


    public List<InNumStatisticsVO> getInNumStatistics(String beginTime, String endTime){
        return litemallMerchandiseMapperEx.getInNumStatistics(beginTime,endTime);
    }

    public List<ConsumerStatisticsVO> getConsumerStatistics(String beginTime, String endTime){
        return litemallMerchandiseMapperEx.getConsumerStatistics(beginTime,endTime);
    }

    public List<LossStatisticsVO> getLossStatistics(String beginTime, String endTime){
        return litemallMerchandiseMapperEx.getLossStatistics(beginTime,endTime);
    }

    public List<LitemallMerchandise> selectList(LambdaQueryWrapper<LitemallMerchandise> baseQuery){
        return litemallMerchandiseMapper.selectList(baseQuery);
    }



    /**
     * 查询选择性
     *
     * @param name          名字
     * @param merchandiseSn 商品sn
     * @param page          页面
     * @param limit         限制
     * @param sort          排序
     * @param order         订单
     * @return {@link List}<{@link LitemallMerchandise}>
     */
    public List<LitemallMerchandise> querySelective(String name, String merchandiseSn,
                                                        Integer page, Integer limit, String sort, String order) {
        LitemallMerchandiseExample example = new LitemallMerchandiseExample();
        LitemallMerchandiseExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(merchandiseSn)) {
            criteria.andMerchandiseSnLike("%" + merchandiseSn + "%");
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        //PageHelper.startPage(page, limit);
        return litemallMerchandiseMapper.selectByExampleSelective(example);
    }

    /**
     * 更新通过id
     *
     * @param merchandise 商品
     * @return
     */
    public boolean updateById(LitemallMerchandise merchandise) {
        merchandise.setUpdateTime(LocalDateTime.now());
        litemallMerchandiseMapper.updateById(merchandise);
        return true;
    }

    /**
     * 删除通过id
     *
     * @param merchandise 商品
     */
    public void deleteById(LitemallMerchandise merchandise) {
        LitemallMerchandiseExample merchandiseExample = new LitemallMerchandiseExample();
        merchandiseExample.or().andIdEqualTo(merchandise.getId());

        merchandise.setDeleted(true);
        merchandise.setUpdateTime(LocalDateTime.now());
        merchandise.setUpdateTime(LocalDateTime.now());
        litemallMerchandiseMapper.updateByExampleSelective(merchandise, merchandiseExample);
    }

    /**
     * 创建
     *
     * @param litemallMerchandise litemall商品
     */
    public void create(LitemallMerchandise litemallMerchandise){
        litemallMerchandise.setAddTime(LocalDateTime.now());
        litemallMerchandise.setUpdateTime(LocalDateTime.now());
        litemallMerchandiseMapper.insert(litemallMerchandise);
    }

    /**
     * 发现通过id
     *
     * @param merchandiseId 商品id
     * @return {@link LitemallMerchandise}
     */
    public LitemallMerchandise findById(Integer merchandiseId){
        return litemallMerchandiseMapper.selectByPrimaryKeyWithLogicalDelete(merchandiseId, false);
    }

    public LitemallMerchandise findByName(String merchandiseName){
        return litemallMerchandiseMapper.selectOne(Wrappers.lambdaQuery(LitemallMerchandise.class).eq(LitemallMerchandise::getName,merchandiseName));
    }

    public List<LitemallMerchandise> findByNameList(String merchandiseName){
        return litemallMerchandiseMapper.selectList(Wrappers.lambdaQuery(LitemallMerchandise.class).eq(LitemallMerchandise::getName,merchandiseName));
    }

    /**
     * 所有
     *
     * @return {@link List}<{@link LitemallMerchandise}>
     */
    public List<LitemallMerchandise> all() {
        return this.selectList(null);
    }

    /**
     * 数
     *
     * @return {@link Integer}
     */
    public long count() {
        LitemallMerchandiseExample merchandiseExample = new LitemallMerchandiseExample();
        merchandiseExample.or().andDeletedEqualTo(false);
        return (int)litemallMerchandiseMapper.countByExample(merchandiseExample);
    }

    /**
     * 查询sn
     *
     * @param merchandiseSn 商品sn
     * @return {@link LitemallMerchandise}
     */
    public LitemallMerchandise queryBySn(String merchandiseSn) {
        LitemallMerchandiseExample merchandiseExample = new LitemallMerchandiseExample();
        merchandiseExample.or().andMerchandiseSnEqualTo(merchandiseSn).andDeletedEqualTo(false);
        return litemallMerchandiseMapper.selectOneByExampleSelective(merchandiseExample);
    }

    /**
     * 查询选择性
     *
     * @param name          名字
     * @param merchandiseSn 商品sn
     * @return {@link List}<{@link LitemallMerchandise}>
     */
    public List<LitemallMerchandise> querySelective(String name, String merchandiseSn) {
		LitemallMerchandiseExample example = new LitemallMerchandiseExample();
		LitemallMerchandiseExample.Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(merchandiseSn)) {
		criteria.andMerchandiseSnLike("%" + merchandiseSn + "%");
		}
		if (!StringUtils.isEmpty(name)) {
		criteria.andNameLike("%" + name + "%");
		}
		criteria.andDeletedEqualTo(false);
		return litemallMerchandiseMapper.selectByExampleSelective(example);
    }

    /**
     * 查询商品id
     *
     * @param merchandiseIds 商品id
     * @return {@link List}<{@link LitemallMerchandise}>
     */
    public List<LitemallMerchandise> queryNotInMerchandiseIds(List<Integer> merchandiseIds) {
		LitemallMerchandiseExample example = new LitemallMerchandiseExample();
		LitemallMerchandiseExample.Criteria criteria = example.createCriteria();
		if(!CollectionUtils.isEmpty(merchandiseIds)){
			criteria.andIdNotIn(merchandiseIds);
		}
		criteria.andDeletedEqualTo(false);
		return litemallMerchandiseMapper.selectByExampleSelective(example);
    }

    /**
     * 统计数据
     *
     * @param merchandiseName 商品名称
     * @param merchandiseSn   商品sn
     * @param beginTime       开始时间
     * @param endTime         结束时间
     * @param page            页面
     * @param limit           限制
     * @return {@link List}<{@link MerchandiseStatisticsVo}>
     */
    public IPage<MerchandiseStatisticsVo> statistics(String merchandiseName,
                                                     String merchandiseSn, String beginTime, String endTime, int page, int limit) {
    	//PageHelper.startPage(page, limit);
    	return litemallMerchandiseMapperEx.statistics(new Page(page,limit),merchandiseName, merchandiseSn, beginTime, endTime);
    }



}
